<?php

namespace app\communal\controller;

use app\model\Question as ModelQuestion;
use app\model\QuestionClass as ModelQuestionClass;
use app\logic\Page AS LogicPage;

class QuestionForAdmin extends Question
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 添加、更新问题分类 \n
     * URI : /communal/admin.question.class.action
     */
    public function actionQuestionClass()
    {
        $str_action_name = $this->_getContentParams('action');
        _toCompare(in_array($str_action_name, ['add', 'update']), '===', false, 13007);

        $str_module_name = $this->_getContentParams('moduleName');
        _toCompare(in_array($str_module_name, $this->arr_module_name_key), '===', false, 19030); // 模块名称不正确

        $str_class_name = $this->_getContentParams('name');
        _toCompare(empty($str_class_name), '===', true, 13004);
        $str_class_image = $this->_getContentParams('image');

        $int_is_show = $this->checkIsShowParam();

        $int_sort = $this->checkSortParam();

        $arr_data = [
            'client' => $str_module_name,
            'name' => $str_class_name,
            'image' => $str_class_image,
            'is_show' => $int_is_show,
            'sort' => $int_sort,
        ];

        if ($str_action_name == 'update') {
            $int_class_id = $this->_getContentParams('classId');
            $bool_result = ModelQuestionClass::getInstance()->checkId($int_class_id, $str_module_name);
            _toCompare($bool_result, '==', false, 13003);

            ModelQuestionClass::getInstance()->toUpdate(['id' => $int_class_id], $arr_data);
        } else {
            ModelQuestionClass::getInstance()->toAdd($arr_data);
        }

        _returnData();
    }

    /**
     * 获取问题分类详情 \n
     * URI : /communal/admin.question.class.info
     */
    public function getQuestionClassInfo()
    {
        $str_class_id = $this->_getContentParams('classId');
        $arr_info = ModelQuestionClass::getInstance()->toFind(['id' => $str_class_id], 'id AS classId,client,name,image,is_show AS isShow,sort');
        _toCompare(isset($arr_info['classId']), '===', false, 13003);
        _returnData($arr_info);
    }

    /**
     * 更新问题分类显示状态 \n
     * URI : /communal/admin.question.class.show.update
     */
    public function updateQuestionClassShow()
    {
        $int_class_id = $this->_getContentParams('classId');
        $this->updateIsShow(ModelQuestionClass::getInstance(), $int_class_id, 13003);
    }

    /**
     * 修改问题分类排序 \n
     * URI : /communal/admin.question.class.sort.update
     */
    public function updateQuestionClassSort()
    {
        $int_class_id = $this->_getContentParams('classId');
        $this->updateSort(ModelQuestionClass::getInstance(), $int_class_id, 13003);
    }

    /**
     * 获取问题分类列表 \n
     * URI : /communal/admin.question.class.list
     */
    public function getQuestionClassList()
    {
        $arr_where = [];
        $int_now_page = $this->_getContentParams('page');
        $int_list_rows = $this->_getContentParams('listRows');;
        $str_field = 'id AS classId,client,name,is_show AS isShow,sort,gmt_create AS gmtCreate';
        $obj = ModelQuestionClass::getInstance();
        $arr_list = LogicPage::getInstance()->getList($obj, $arr_where, $str_field, $int_now_page, $int_list_rows, 'sort ASC');
        if (isset($arr_list['list']) && empty($arr_list['list']) === false) {
            foreach ($arr_list['list'] as &$value) {
                $value['moduleName'] = isset($this->arr_module_name[$value['client']]) ? $this->arr_module_name[$value['client']] : '';
                unset($value['class_id'], $value['client']);
            }
        }
        _returnData($arr_list);
    }

    /**
     * 删除问题分类 \n
     * URI : /communal/admin.question.class.delete
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     * classId     | string | 必填  | 编号（id）(例：classId='1'  classId='1,2,3...')
     *
     * @return :
     *  name  | type | description
     * -------|------|-------------
     *  ----- | ---- | 无
     *
     */
    public function deleteQuestionClassInfo()
    {
        $int_class_id = $this->_getContentParams('classId');
        $bool_result = ModelQuestionClass::getInstance()->checkId($int_class_id);
        _toCompare($bool_result, '==', false, 13003);

        $bool_res = ModelQuestion::getInstance()->checkQuestionClass($int_class_id);
        _toCompare($bool_res, '!=', false, 13009);

        ModelQuestionClass::getInstance()->toDelete(['id' => $int_class_id]);
        _returnData();
    }

    /**
     * 添加、更新问题 \n
     * URI : /communal/admin.question.action
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     * title       | string | 必填  | 标题
     * className   | string | 必填  | 分类名称
     * content     | text   | 选填  | 内容
     * isShow      | int    | 必填  | 是否显示  1否 2是 (例：isShow=1)
     *
     * @return :
     *  name         | type   | description
     * --------------|--------|-------------
     *  ------------ | ------ | 无
     *
     */
    public function actionQuestionInfo()
    {
        $str_action_name = $this->_getContentParams('action');
        _toCompare(in_array($str_action_name, ['add', 'update']), '===', false, 13007);

        $str_module_name = $this->_getContentParams('moduleName');
        _toCompare(in_array($str_module_name, $this->arr_module_name_key), '===', false, 19030); // 模块名称不正确

        $int_class_id = $this->_getContentParams('classId');
        $bool_result = ModelQuestionClass::getInstance()->checkId($int_class_id, $str_module_name);
        _toCompare($bool_result, '==', false, 13003);

        $str_title = $this->_getContentParams('title');
        _toCompare(empty($str_title), '===', true, 10101);

        $str_content = $this->_getContentParams('content');
        _toCompare(empty($str_content), '===', true, 13001);

        $int_is_show = $this->checkIsShowParam();

        $int_sort = $this->checkSortParam();

        $arr_data = [
            'client' => $str_module_name,
            'class_id' => $int_class_id,
            'title' => $str_title,
            'content' => $str_content,
            'is_show' => $int_is_show,
            'sort' => $int_sort,
        ];

        if ($str_action_name == 'update') {
            $int_question_id = $this->_getContentParams('questionId');
            $bool_result = ModelQuestion::getInstance()->checkId($int_question_id, $str_module_name);
            _toCompare($bool_result, '==', false, 13010);

            ModelQuestion::getInstance()->toUpdate(['id' => $int_class_id], $arr_data);
        } else {
            ModelQuestion::getInstance()->toAdd($arr_data);
        }

        _returnData();
    }

    /**
     * 获取问题分类列表 \n
     * URI : /communal/admin.question.list
     */
    public function getQuestionList()
    {
        $arr_where = [];
        $int_now_page = $this->_getContentParams('page');
        $int_list_rows = $this->_getContentParams('listRows');
        $str_field = 'id AS questionId,client,title,class_id,is_show AS isShow,sort,gmt_create AS gmtCreate';
        $obj = ModelQuestion::getInstance();
        $arr_list = LogicPage::getInstance()->getList($obj, $arr_where, $str_field, $int_now_page, $int_list_rows, 'sort ASC');
        if (isset($arr_list['list']) && empty($arr_list['list']) === false) {
            foreach ($arr_list['list'] as &$value) {
                $value['className'] = ModelQuestionClass::getInstance()->toValue(['id' => $value['class_id']], 'name');
                $value['moduleName'] = isset($this->arr_module_name[$value['client']]) ? $this->arr_module_name[$value['client']] : '';
                unset($value['class_id'], $value['client']);
            }
        }
        _returnData($arr_list);
    }

    /**
     * 获取问题详情（详情） \n
     * URI : /communal/admin.question.info
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     * classId     | int    | 必填  | 编号（id）
     *
     * @return :
     *  name       | type   | description
     * ------------|--------|-------------
     *  classImage | string | 分类图标
     *  className  | string | 分类名称
     *  isShow     | int    | 是否显示  1否 2是
     *
     */
    public function getQuestionInfo()
    {
        $str_question_id = $this->_getContentParams('questionId');
        $str_field = 'id AS questionId,class_id AS classId,client,title,content,is_show AS isShow,sort';
        $arr_info = ModelQuestion::getInstance()->toFind(['id' => $str_question_id], $str_field);
        _toCompare(isset($arr_info['questionId']), '===', false, 13010);
        _returnData($arr_info);
    }

    /**
     * 更新问题显示状态 \n
     * URI : /communal/admin.question.show.update
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     * classId     | string | 必填  | 编号（id）  (例：classId='1' 或 classId='1,2,3...')
     * isShow      | int    | 必填  | 是否显示  1否 2是 (例：isShow=1)
     *
     * @return :
     *  name         | type   | description
     * --------------|--------|-------------
     *  ------------ | ------ | 无
     *
     */
    public function updateQuestionShow()
    {
        $int_question_id = $this->_getContentParams('questionId');
        $this->updateIsShow(ModelQuestion::getInstance(), $int_question_id, 13010);
    }

    /**
     * 修改问题排序 \n
     * URI : /communal/admin.question.sort.update
     * @param :
     *  name       | type   | null | description
     * ------------|--------|------|-------------
     * classId     | int    | 必填  | 编号（id）
     * sort        | int    | 必填  | 排序（大于0）
     *
     * @return :
     *  name  | type | description
     * -------|------|-------------
     *  ----- | ---- | 无
     *
     */
    public function updateQuestionSort()
    {
        $int_question_id = $this->_getContentParams('questionId');
        $this->updateSort(ModelQuestion::getInstance(), $int_question_id, 13010);
    }

    /**
     * 删除问题 \n
     * URI : /communal/admin.question.delete
     */
    public function deleteQuestionInfo()
    {
        $int_question_id = $this->_getContentParams('questionId');
        $this->deleteData(ModelQuestion::getInstance(), $int_question_id, 13010);
    }
}
